public class Test03 {
    public class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }

    class Solution {
        public ListNode swapPairs(ListNode head) {
            //处理边界条件
            if (head == null || head.next == null) {
                return head;
            }
            //返回值
            ListNode ret = head.next;
            //需要定义一个前驱
            ListNode prev = null;
            while (head != null && head.next != null) {
                ListNode cur = head.next;
                ListNode curNext = cur.next;
                //进行交换
                cur.next = head;
                head.next = curNext;
                if (prev != null) {
                    prev.next = cur;
                }
                prev = head;
                head = curNext;

            }
            return ret;
        }
    }

    public static void main(String[] args) {

    }
}
